package org.ceteca.explica.client.partners;

import java.text.DecimalFormat;
import java.util.HashSet;

import org.ceteca.explica.client.ClientContext;
import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.InterfaceErrors;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.MultiSelectionSubentityOT;
import org.ceteca.explica.core.common.SelectionSubentityOT;
import org.ceteca.explica.core.partners.BankAccountOT;
import org.ceteca.explica.core.partners.ContactOT;
import org.ceteca.explica.core.partners.InterfacePartners;
import org.ceteca.explica.core.partners.PartnerOT;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.ceteca.explica.core.util.Validator;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class BankAccountClient extends CommonClient {

	// WIDGETS
	Text entryBankAccountCode, entryBankAccountDescription,
			entryBankAccountIdBank, entryBankAccountIdBranch,
			entryBankAccountControlNumber, entryBankAccountAccountNumber,
			entryBankAccountPartner, entryVersion;
	Button buttonBankAccountPartner;

	
	
	
	public BankAccountClient() {
		super(BankAccountOT.class,
				InterfacePartners.EXPLICA_EDIT_BANKACCOUNT_GLADE_PATH,

				InterfacePartners.EDIT_BANKACCOUNT_DIALOG_NAME);
	}

	
	
	
	
	@Override
	public BaseVO getEntityData() {

		BankAccountOT entity = new BankAccountOT();
		if (this.entEditView != null) {
			this.getWidgets();
			entity.setDescription(entryBankAccountDescription.getText());
			entity.setIdBank(Converter.getInstance().parseLong(
					entryBankAccountIdBank.getText()));
			entity.setIdBranch(Converter.getInstance().parseLong(
					entryBankAccountIdBranch.getText()));
			entity.setAccountNumber(Converter.getInstance().parseLong(
					entryBankAccountAccountNumber.getText()));
			entity.setControlNumber(Converter.getInstance().parseLong(
					entryBankAccountControlNumber.getText()));
			entity.setPartner((PartnerOT) entryBankAccountPartner.getData());
			
			if (selectedEntity != null) {
				entity.setVersion(((BankAccountOT) this.selectedEntity)
						.getVersion());
				entity.setIdBankAccount(((BankAccountOT) this.selectedEntity)
						.getIdBankAccount());
			}
		}

		return entity;
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	public void setWidgetsValues(BaseVO entity) {

		BankAccountOT ent = (BankAccountOT) entity;
		DecimalFormat df1 = new DecimalFormat("0000");
		DecimalFormat df2 = new DecimalFormat("00");
		DecimalFormat df3 = new DecimalFormat("0000000000");
		getWidgets();
		entryBankAccountCode.setText(Converter.getInstance().toString(
				ent.getIdBankAccount()));
		entryBankAccountDescription.setText(ent.getDescription()==null?"":ent.getDescription());
		entryBankAccountIdBank.setText(df1.format(ent.getIdBank()));
		entryBankAccountIdBranch.setText(df1.format(ent.getIdBranch()));
		entryBankAccountControlNumber.setText(df2
				.format(ent.getControlNumber()));
		entryBankAccountAccountNumber.setText(df3
				.format(ent.getAccountNumber()));
		entryVersion.setText(Converter.getInstance().toString(ent.getVersion()));
		if (ent.getPartner() != null) {
			entryBankAccountPartner.setText(ent.getPartner().getName()==null?"":ent.getPartner().getName());
			entryBankAccountPartner.setData(ent.getPartner());
		}
	}

	public OperationResult validateEntity() {
		OperationResult result = new OperationResult();
		boolean datosValidos = true;
		BankAccountOT entity = (BankAccountOT) getEntityData();
		if (Validator.getInstance().isValidBankAccount(entity.getIdBank(),
				entity.getIdBranch(), entity.getControlNumber(),
				entity.getAccountNumber()))
			datosValidos = false;
		if (datosValidos)
			result.addResultMessage(OperationResult.RESULT_OK, ClientContext
					.getInstance().getMsgCatalog().newResultMessage(
							InterfaceErrors.MSG_VALIDATE_OK));
		else
			result.addResultMessage(OperationResult.RESULT_ERROR, ClientContext
					.getInstance().getMsgCatalog().newResultMessage(
							InterfaceErrors.MSG_VALIDATE_ERROR));
		return result;
	}

	@Override
	public void getWidgets() {

		boolean allOk = true;

		allOk = ((entryBankAccountCode = ((Text) this.getWidget(
				InterfacePartners.EDIT_BANKACCOUNT_CODE_WIDGETNAME))) != null)
				&&

				((entryBankAccountDescription = ((Text) this
						.getWidget(
								InterfacePartners.EDIT_BANKACCOUNT_DESCRIPTION_WIDGETNAME))) != null)
				&&

				((entryBankAccountIdBank = (Text) this.getWidget(
						InterfacePartners.EDIT_BANKACCOUNT_IDBANK_WIDGETNAME)) != null)
				&&

				((entryBankAccountIdBranch = (Text) this.getWidget(
						InterfacePartners.EDIT_BANKACCOUNT_IDBRANCH_WIDGETNAME)) != null)
				&&

				((entryBankAccountControlNumber = (Text) this
						.getWidget(
								InterfacePartners.EDIT_BANKACCOUNT_CONTROLNUMBER_WIDGETNAME)) != null)
				&&

				((entryBankAccountPartner = (Text) this.getWidget(
						InterfacePartners.EDIT_BANKACCOUNT_PARTNER_WIDGETNAME)) != null)
						
				&&

				((entryVersion = (Text) this.getWidget(
						InterfacePartners.EDIT_VERSION_WIDGETNAME)) != null)		

										
				&&

				((buttonBankAccountPartner = (Button) this.getWidget(
						InterfacePartners.EDIT_BANKACCOUNT_PARTNER_SELECT_BUTTON_WIDGETNAME)) != null)			
						
				&&

				((entryBankAccountAccountNumber = (Text) this
						.getWidget(
								InterfacePartners.EDIT_BANKACCOUNT_ACCOUNTNUMBER_WIDGETNAME)) != null);

		if (!allOk) {
			this.logger
					.error("Error getting entry Bank Account Account  Widget");
			System.exit(-1);
		}

	}

	@Override
	public void setEditionMode(boolean enabled) {
		getWidgets();
		entryBankAccountAccountNumber.setEnabled(enabled);
		entryBankAccountCode.setEnabled(false);
		entryBankAccountControlNumber.setEnabled(enabled);
		entryBankAccountDescription.setEnabled(enabled);
		entryBankAccountIdBank.setEnabled(enabled);
		entryBankAccountIdBranch.setEnabled(enabled);
		entryVersion.setEnabled(false);
		entryBankAccountPartner.setEnabled(enabled);
		buttonBankAccountPartner.setEnabled(enabled);
		if (isDetail()) {
			if (this.entityDetailView.getSubentityData().getParentEntity()
					.getEntity().getKey().equals(
							InterfacePartners.EXPLICA_ENTITY_PARTNER_ID)) {
				entryBankAccountPartner.setEnabled(false);
				buttonBankAccountPartner.setEnabled(false);
			}
		}
	}

	@Override
	public void buildSelectionSets() {
		lSelectionSubentities = new HashSet<SelectionSubentityOT>();
		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_PARTNER_ID,
				InterfacePartners.EDIT_BANKACCOUNT_PARTNER_WIDGETNAME, this,
				buttonBankAccountPartner));

		lMultiSelectionSubentities = new HashSet<MultiSelectionSubentityOT>();
	}

	

	@Override
	public void clearEntityData() {
		BankAccountOT ent = new BankAccountOT();
		if (isDetail()) {
			if (this.entityDetailView.getSubentityData().getParentEntity()
					.getEntity().getKey().equals(
							InterfacePartners.EXPLICA_ENTITY_PARTNER_ID)) {
				PartnerOT parent = (PartnerOT) entityDetailView
						.getSubentityData().getParentEntity()
						.getSelectedEntity();
				ent.setPartner(parent);
				entryBankAccountPartner.setEnabled(false);
				buttonBankAccountPartner.setEnabled(false);

			}
		} else {
			entryBankAccountPartner.setText("");
			entryBankAccountPartner.setEnabled(false);
			buttonBankAccountPartner.setEnabled(false);
		}
		setWidgetsValues(ent);

	}
	
	
	@Override
	public void init_specific_components() {
		// TODO Auto-generated method stub

	}





	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void fillEntityData() {
		if (this.selectedEntity != null && this.entEditView != null) {
			getWidgets();
			setWidgetsValues((BankAccountOT) selectedEntity);
		}
	}

}
